package com.heihei.mymusic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heihei.mymusic.dto.CommonResponseDTO;
import com.heihei.mymusic.entity.MyMusic;
import com.heihei.mymusic.mapper.MyMusicMapper;
import com.heihei.mymusic.service.MyMusicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heihei.mymusic.util.PageUtil;
import com.heihei.mymusic.vo.MyMusicPageVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author duanhengfei
 * @since 2020-12-11
 */
@Service
public class MyMusicServiceImpl extends ServiceImpl<MyMusicMapper, MyMusic> implements MyMusicService {


    /**
     * 查询
     * @param pageParam
     * @return
     */
    @Override
    public CommonResponseDTO<MyMusicPageVo<MyMusic>> listConditionalPage(MyMusicPageVo<MyMusic> pageParam) {
        //分页信息
        Page page = PageUtil.getPage(pageParam);
        LambdaQueryWrapper<MyMusic> queryWrapper = new LambdaQueryWrapper<>();
        //条件查询信息
        if(pageParam.getCurrSelectedTypeId() != null) {
            queryWrapper.eq(MyMusic::getType, pageParam.getCurrSelectedTypeId());
        }
        if(StringUtils.isNotEmpty(pageParam.getSearchWords())) {
            queryWrapper.like(MyMusic::getName, pageParam.getSearchWords());
        }
        IPage<MyMusic> queryResult = page(page, queryWrapper);
        //转换成最终的数据格式
        transResult(pageParam, queryResult);
        return CommonResponseDTO.forSuccess(pageParam);
    }


    /**
     * 转换返回结果
     * @param queryResult
     * @return
     */
    private void transResult(MyMusicPageVo<MyMusic> pageParam, IPage<MyMusic> queryResult) {
        pageParam.setTotalCount(queryResult.getTotal());
        pageParam.setCurrentPage(queryResult.getCurrent());
        pageParam.setPageSize(queryResult.getSize());
        pageParam.setDataList(queryResult.getRecords());
    }
}
